package com.dream.keigezhushou.Activity.activity;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.LingshiInfo;
import com.dream.keigezhushou.Activity.bean.NearKtvDetilsInfo;
import com.dream.keigezhushou.Activity.bean.TeamInfo;
import com.dream.keigezhushou.Activity.bean.TimeInfo;
import com.dream.keigezhushou.Activity.wigdt.GradationScrollView;
import com.dream.keigezhushou.Activity.wigdt.MyListView;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class KtvDetailsActivity extends AppCompatActivity implements GradationScrollView.ScrollViewListener {

    @BindView(R.id.shoucang)
    ImageView mShoucang;//收藏
    @BindView(R.id.share)
    ImageView mShare;//分享
    //    @BindView(R.id.ivicon)
//    ImageView mIvicon;//ktv图片
    @BindView(R.id.ktvtitle)
    TextView mKtvtitle;//ktv名称
    @BindView(R.id.ratingBar_1)
    RatingBar mRatingBar1;//星评分
    @BindView(R.id.pf)
    TextView mPf;//数字分
    @BindView(R.id.meanprice)
    TextView mMeanprice;//均价

    @BindView(R.id.location)
    TextView mLocation;//地址
    @BindView(R.id.phone)
    ImageView mPhone;//电话
    @BindView(R.id.personnumber)
    TextView mPersonnumber;//人数已预订
    @BindView(R.id.tv_1)
    TextView mTv1;//
    @BindView(R.id.data_1)
    TextView mData1;
    @BindView(R.id.view_1)
    View mView1;
    @BindView(R.id.time1)
    RelativeLayout mTime1;
    @BindView(R.id.data_2)
    TextView mData2;
    @BindView(R.id.view_2)
    View mView2;
    @BindView(R.id.tv_2)
    TextView mTv2;
    @BindView(R.id.time2)
    RelativeLayout mTime2;
    @BindView(R.id.tv_3)
    TextView mTv3;
    @BindView(R.id.data_3)
    TextView mData3;
    @BindView(R.id.view_3)
    View mView3;
    @BindView(R.id.time3)
    RelativeLayout mTime3;
    @BindView(R.id.tv_4)
    TextView mTv4;
    @BindView(R.id.data_4)
    TextView mData4;
    @BindView(R.id.view_4)
    View mView4;
    @BindView(R.id.time4)
    RelativeLayout mTime4;
    @BindView(R.id.tv_5)
    TextView mTv5;
    @BindView(R.id.data_5)
    TextView mData5;
    @BindView(R.id.view_5)
    View mView5;
    @BindView(R.id.time5)
    RelativeLayout mTime5;
    @BindView(R.id.tv_6)
    TextView mTv6;
    @BindView(R.id.data_6)
    TextView mData6;
    @BindView(R.id.view_6)
    View mView6;
    @BindView(R.id.time6)
    RelativeLayout mTime6;
    @BindView(R.id.tv_7)
    TextView mTv7;
    @BindView(R.id.data_7)
    TextView mData7;
    @BindView(R.id.view_7)
    View mView7;
    @BindView(R.id.time7)
    RelativeLayout mTime7;
    @BindView(R.id.type_1)
    TextView mType1;
    @BindView(R.id.type_2)
    TextView mType2;
    @BindView(R.id.type_3)
    TextView mType3;
    @BindView(R.id.timelist)
    MyListView mTimelist;//时间段集合
    @BindView(R.id.team)
    TextView mTeam;//团购
    @BindView(R.id.scattered)
    TextView mScattered;//零食
    @BindView(R.id.tv_js)
    TextView mTvJs;//酒水
    @BindView(R.id.tv_ls)
    TextView mTvLs;//零食
    @BindView(R.id.tv_sg)
    TextView mTvSg;//水果
    @BindView(R.id.tv_qt)
    TextView mTvQt;//其他
    @BindView(R.id.teamlist)
    MyListView mTeamlist;//团购集合
    @BindView(R.id.ktvlist)
    MyListView mKtvlist;//附近Ktv集合

    @BindView(R.id.back)
    ImageView mBack;//返回
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.rj)
    TextView mRj;
    @BindView(R.id.yj)
    TextView mYj;
    @BindView(R.id.actionbar)
    RelativeLayout mActionbar;

    @BindView(R.id.ll)
    LinearLayout mLl;
    @BindView(R.id.activity_ktv_details)
    LinearLayout mActivityKtvDetails;
    @BindView(R.id.timeparent)
    LinearLayout mTimeparent;
    @BindView(R.id.scrollview)
    GradationScrollView mScrollview;
    @BindView(R.id.rl)
    RelativeLayout mRl;
    @BindView(R.id.iv)
    RelativeLayout mIv;

    private ArrayList<TimeInfo> timeList = new ArrayList<>();
    private ArrayList<TeamInfo> teamList = new ArrayList<>();
    private ArrayList<LingshiInfo> lingshiList = new ArrayList<>();
    private ArrayList<NearKtvDetilsInfo> nearktvList = new ArrayList<>();
    private int imageHeight;
    private LayoutInflater inflater;
    private LinearLayout headView;
    private RelativeLayout loadmore;
    private TextView check;
    private ArrayList<TeamInfo> mShowObjects = new ArrayList<>();
    private ArrayList<TeamInfo> mAllObjects = null;
    public static final int DEFAULT_SHOW_COUNT = 2;
    private Context mContext;
    private FootviewAdapter mMFootviewAdapter;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);
        }

//        Window window = getWindow();
//        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
//        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//
//        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
//        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
//        //设置状态栏颜色
//        window.setStatusBarColor(getResources().getColor(R.color.seting));
//
//        ViewGroup mContentView = (ViewGroup) findViewById(Window.ID_ANDROID_CONTENT);
//        View mChildView = mContentView.getChildAt(0);
//        if (mChildView != null) {
//            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
//            ViewCompat.setFitsSystemWindows(mChildView, t);
//        }
        setContentView(R.layout.activity_ktv_details);


        ButterKnife.bind(this);
        initListeners();
        initdata();
//        setListViewHeightBasedOnChildren(mTimelist);
//        setListViewHeightBasedOnChildren(mTeamlist);
//        setListViewHeightBasedOnChildren(mKtvlist);


        //加载动画
        //获取数据url
        //假数据

        mTimelist.setAdapter(new TimeAdapter());

        mMFootviewAdapter = new FootviewAdapter(KtvDetailsActivity.this,mTeamlist);
        mTeamlist.setAdapter(mMFootviewAdapter);
        mMFootviewAdapter.setAdapterData(teamList);
        // mTeamlist.setAdapter(new TeamAdapter());
        mKtvlist.setAdapter(new MyAdapter());

        mTeamlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //传数据


                //跳页面
                Intent intent = new Intent(KtvDetailsActivity.this, TeamBuyDetailsActivity.class);

                startActivity(intent);


            }
        });

        mKtvlist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                //跳页面
                Intent intent = new Intent(KtvDetailsActivity.this, KtvDetailsActivity.class);

                startActivity(intent);

            }
        });


    }


//    private void setListViewHeightBasedOnChildren(ListView listView) {
//        ListAdapter listAdapter = listView.getAdapter();
//        if (listAdapter == null) {
//            // pre-condition
//            return;
//        }
//
//        int totalHeight = 0;
//        for (int i = 0; i < listAdapter.getCount(); i++) {
//            View listItem = listAdapter.getView(i, null, listView);
//            listItem.measure(0, 0);
//            totalHeight += listItem.getMeasuredHeight();
//        }
//
//        ViewGroup.LayoutParams params = listView.getLayoutParams();
//        params.height = totalHeight
//                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
//        listView.setLayoutParams(params);
//    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransition(R.anim.from_right_in, R.anim.to_left_out);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.from_left_in, R.anim.to_right_out);
    }


    private void initListeners() {

        ViewTreeObserver vto = mIv.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mIv.getViewTreeObserver().removeGlobalOnLayoutListener(
                        this);
                imageHeight = mIv.getHeight();

                mScrollview.setScrollViewListener(KtvDetailsActivity.this);
            }
        });
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onScrollChanged(GradationScrollView scrollView, int x, int y, int oldx, int oldy) {


        Window window = getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
                | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

        if (y <= 0) {   //设置标题的背景颜色
            window.setStatusBarColor(Color.TRANSPARENT);
            window.setNavigationBarColor(Color.TRANSPARENT);

            mRl.setBackgroundColor(Color.argb((int) 0, 144, 151, 166));

        } else if (y > 0 && y <= imageHeight) { //滑动距离小于banner图的高度时，设置背景和字体颜色颜色透明度渐变
            float scale = (float) y / imageHeight;
            float alpha = (255 * scale);
            // mRl.setTextColor(Color.argb((int) alpha, 255, 255, 255));
            mRl.setBackgroundColor(Color.argb((int) alpha, 144, 151, 166));
            window.setStatusBarColor(Color.argb((int) alpha, 144, 151, 166));
            window.setNavigationBarColor(Color.argb((int) alpha, 144, 151, 166));

        } else {    //滑动到banner下面设置普通颜色
            mRl.setBackgroundColor(Color.argb((int) 255, 255, 255, 255));
            window.setStatusBarColor(Color.argb((int) 255, 255, 255, 255));
            window.setNavigationBarColor(Color.argb((int) 255, 255, 255, 255));

        }
    }

    private void initdata() {
        // mIvicon.setImageDrawable(getResources().getDrawable(R.mipmap.ivicon));
        mKtvtitle.setText("乐唱好声音KTV");
        mRatingBar1.setRating(4.0f);
        mPf.setText("4" + "分");
        mMeanprice.setText("50");
        mLocation.setText("未央区凤城二路东段（近未央路）");
        mPersonnumber.setText("2937" + "人订过");
        mTime1.setSelected(true);
        mType1.setSelected(true);
        mTeam.setSelected(true);
        mView1.setVisibility(View.VISIBLE);
        for (int i = 0; i < 5; i++) {
            TimeInfo info = new TimeInfo();
            info.setDetails("欢唱6小时+套餐");
            info.setPrice("68");
            info.setTimes("13:00-19:00");
            timeList.add(info);
        }

        for (int g = 0; g < 6; g++) {
            NearKtvDetilsInfo info = new NearKtvDetilsInfo();
            info.setCombo("［未央路］周一至周日时段3选1欢唱＋小吃饮品套餐");
            info.setSaleprice("794");
            info.setPrice("188");
            info.setDistance("<500m");
            info.setName("乐唱好声音KTV");
            info.setIv(getResources().getDrawable(R.mipmap.iconiv));

            nearktvList.add(info);
        }
        for (int j = 0; j < 10; j++) {
            TeamInfo info = new TeamInfo();
            info.setPrice("188");
            info.setCombo("周一至周日时段3选1欢唱+小吃饮品套餐");
            info.setSaleprice("794");
            info.setYishou("2025");
            info.setIv(getResources().getDrawable(R.mipmap.team_icon));
            teamList.add(info);

        }
        for (int k = 0; k < 6; k++) {
            LingshiInfo info = new LingshiInfo();
            info.setIv(getResources().getDrawable(R.mipmap.ivls));
            info.setPrice("66.66");
            info.setTvname("原装青岛纯生250ml＊1瓶");
            lingshiList.add(info);
        }


    }


    @OnClick({R.id.back, R.id.shoucang,R.id.location, R.id.share, R.id.phone, R.id.time1, R.id.time2, R.id.time3, R.id.time4, R.id.time5, R.id.time6, R.id.time7, R.id.type_1, R.id.type_2, R.id.type_3, R.id.team, R.id.scattered, R.id.tv_js, R.id.tv_ls, R.id.tv_sg, R.id.tv_qt, R.id.actionbar})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.shoucang:
                //添加到集合
                UiUtils.toast("收藏成功");
                break;
            case R.id.location:
//                Intent intent = new Intent(KtvDetailsActivity.this, BaiduMap.class);
//                startActivity(intent);
                break;
            case R.id.share:
                //集成sdk

                break;

            case R.id.phone:
                //获取电话号码并拨打

                break;
            case R.id.time1:
                setSelect(0);


                break;
            case R.id.time2:
                setSelect(1);

                break;
            case R.id.time3:
                setSelect(2);
                break;
            case R.id.time4:
                setSelect(3);
                break;
            case R.id.time5:
                setSelect(4);
                break;
            case R.id.time6:
                setSelect(5);
                break;
            case R.id.time7:
                setSelect(6);
                break;
            case R.id.type_1:
                setSelectType(0);

                mTimelist.setAdapter(new TimeAdapter());

                break;
            case R.id.type_2:
                setSelectType(1);
                break;
            case R.id.type_3:
                setSelectType(2);
                break;
            case R.id.team:
                setSelectNum(3);
                mLl.setVisibility(View.GONE);
                mTeamlist.setAdapter(mMFootviewAdapter);
                mMFootviewAdapter.setAdapterData(teamList);
                //   mTeamlist.setAdapter(new TeamAdapter());
                break;
            case R.id.scattered:
                setSelectNum(4);
                mLl.setVisibility(View.VISIBLE);
                mTvJs.setSelected(true);

                mTeamlist.setAdapter(new LsAdapter());

                break;
            case R.id.tv_js:
                setSelectTypeNum(5);
                break;
            case R.id.tv_ls:
                setSelectTypeNum(6);
                break;
            case R.id.tv_sg:
                setSelectTypeNum(7);
                break;
            case R.id.tv_qt:
                setSelectTypeNum(8);
                break;
            case R.id.actionbar:
                break;

        }


    }

    public void setSelect(int number) {


        mView1.setVisibility(number == 0 ? View.VISIBLE : View.GONE);
        mData1.setSelected(number == 0 ? true : false);
        mTime1.setSelected(number == 0 ? true : false);
        mType1.setSelected(number == 0 ? true : false);
        mView2.setVisibility(number == 1 ? View.VISIBLE : View.GONE);
        mData2.setSelected(number == 1 ? true : false);
        mTime2.setSelected(number == 1 ? true : false);
        mView3.setVisibility(number == 2 ? View.VISIBLE : View.GONE);
        mData3.setSelected(number == 2 ? true : false);
        mTime3.setSelected(number == 2 ? true : false);
        mView4.setVisibility(number == 3 ? View.VISIBLE : View.GONE);
        mData4.setSelected(number == 3 ? true : false);
        mTime4.setSelected(number == 3 ? true : false);
        mView5.setVisibility(number == 4 ? View.VISIBLE : View.GONE);
        mData5.setSelected(number == 4 ? true : false);
        mTime5.setSelected(number == 4 ? true : false);
        mView6.setVisibility(number == 5 ? View.VISIBLE : View.GONE);
        mData6.setSelected(number == 5 ? true : false);
        mTime6.setSelected(number == 5 ? true : false);
        mView7.setVisibility(number == 6 ? View.VISIBLE : View.GONE);
        mData7.setSelected(number == 6 ? true : false);
        mTime7.setSelected(number == 6 ? true : false);

    }

    public void setSelectType(int number) {
        mType1.setSelected(number == 0 ? true : false);
        mType2.setSelected(number == 1 ? true : false);
        mType3.setSelected(number == 2 ? true : false);

    }

    public void setSelectNum(int number) {
        mTeam.setSelected(number == 3 ? true : false);
        mScattered.setSelected(number == 4 ? true : false);
    }

    public void setSelectTypeNum(int number) {
        mTvJs.setSelected(number == 5 ? true : false);
        mTvLs.setSelected(number == 6 ? true : false);
        mTvSg.setSelected(number == 7 ? true : false);
        mTvQt.setSelected(number == 8 ? true : false);
    }


    class TimeAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return timeList.size();
        }

        @Override
        public TimeInfo getItem(int position) {
            return timeList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TimeViewHolder timeholder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_time);
                timeholder = new TimeViewHolder();
                timeholder.times = (TextView) convertView.findViewById(R.id.time_1);
                timeholder.details = (TextView) convertView.findViewById(R.id.des_1);
                timeholder.price = (TextView) convertView.findViewById(R.id.price_1);
                timeholder.yuding = (TextView) convertView.findViewById(R.id.yuding1);
                convertView.setTag(timeholder);
            } else {
                timeholder = (TimeViewHolder) convertView.getTag();
            }

            TimeInfo info = getItem(position);
            timeholder.times.setText(info.getTimes());
            timeholder.price.setText(info.getPrice());
            timeholder.details.setText(info.getDetails());

            timeholder.yuding.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(KtvDetailsActivity.this, YudingbaoxiangActivity.class);

                    startActivity(intent);


                }
            });

            return convertView;
        }
    }

    static class TimeViewHolder {
        TextView times;
        TextView details;
        TextView price;
        TextView yuding;
    }

    class FootviewAdapter extends BaseFootviewAdapter<TeamInfo> {

        private ListItemView listItemView = null;

        public final class ListItemView {
            ImageView iv;
            TextView combo;
            TextView price;
            TextView saleprice;
            TextView yishou;
        }

        public FootviewAdapter(Context context,MyListView mListView) {
            super(context,mListView);
        }



        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TeamInfo item = getItem(position);
            if (convertView == null) {
                convertView = inflater.inflate(R.layout.item_teambuy, parent, false);
                listItemView = new ListItemView();
                creatView(convertView, listItemView);
                convertView.setTag(listItemView);
            } else {
                listItemView = (ListItemView) convertView.getTag();
            }
            listItemView.combo.setText(item.getCombo());
            listItemView.iv.setImageDrawable(item.getIv());
            listItemView.price.setText(item.getPrice());
            listItemView.saleprice.setText(item.getSaleprice());
            listItemView.yishou.setText("已售" + item.getYishou());
            return convertView;
        }

        private void creatView(View convertView, ListItemView listItemView) {
            listItemView.combo = (TextView) convertView.findViewById(R.id.nametv);
            listItemView.iv = (ImageView) convertView.findViewById(R.id.team_iv);
            listItemView.price = (TextView) convertView.findViewById(R.id.price);
            listItemView.saleprice = (TextView) convertView.findViewById(R.id.pricesale);
            listItemView.yishou = (TextView) convertView.findViewById(R.id.yishou);
        }


    }

    class LsAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return lingshiList.size();
        }

        @Override
        public LingshiInfo getItem(int position) {
            return lingshiList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LsViewHolder holder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_lingshi);
                holder = new LsViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.iv);
                holder.price = (TextView) convertView.findViewById(R.id.price);
                holder.tvname = (TextView) convertView.findViewById(R.id.tv);
                holder.add = (TextView) convertView.findViewById(R.id.add);
                convertView.setTag(holder);
            } else {
                holder = (LsViewHolder) convertView.getTag();
            }
            LingshiInfo info = getItem(position);
            holder.iv.setImageDrawable(info.getIv());
            holder.price.setText(info.getPrice());
            holder.tvname.setText(info.getTvname());
            holder.add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UiUtils.toast("添加成功");
                    //逻辑
                    //动画
                }
            });
            return convertView;
        }
    }

    static class LsViewHolder {
        private ImageView iv;
        private TextView tvname;
        private TextView price;
        private TextView add;
    }

    private class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return nearktvList.size();
        }

        @Override
        public NearKtvDetilsInfo getItem(int position) {
            return nearktvList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            if (convertView == null) {
                convertView = UiUtils.inflateView(R.layout.item_merchant_ktvdetails);
                holder = new ViewHolder();
                holder.combo = (TextView) convertView.findViewById(R.id.combo);
                holder.name = (TextView) convertView.findViewById(R.id.ktv_name);
                holder.price = (TextView) convertView.findViewById(R.id.price);
                holder.saleprice = (TextView) convertView.findViewById(R.id.pricesale);
                holder.distance = (TextView) convertView.findViewById(R.id.distance);
                holder.iv = (ImageView) convertView.findViewById(R.id.iv_icon);

                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }

            NearKtvDetilsInfo info = getItem(position);
            holder.iv.setImageDrawable(info.getIv());
            holder.combo.setText(info.getCombo());
            holder.distance.setText(info.getDistance());
            holder.name.setText(info.getName());
            holder.price.setText(info.getPrice());
            holder.saleprice.setText(info.getSaleprice());


            return convertView;
        }
    }

    static class ViewHolder {
        ImageView iv;
        TextView name;
        TextView combo;
        TextView price;
        TextView saleprice;
        TextView distance;

    }

    public abstract class BaseFootviewAdapter<E> extends BaseAdapter {

        //  protected Context mContext;
        //protected MyListView mTeamlist;
        //   protected LayoutInflater inflater;
        //  protected LinearLayout headView;
        //   protected   RelativeLayout loadmore;
        //  private    TextView   check;
//         protected ArrayList<TeamInfo> mShowObjects = new ArrayList<TeamInfo>();

        protected boolean shrink = true;
        protected boolean isForecast =false;
        private ImageView mIv;

        @SuppressWarnings("unused")


        private BaseFootviewAdapter() {

        }


        @SuppressLint("InflateParams")


        public BaseFootviewAdapter( Context context,MyListView mListView) {
            mContext =context;

            mTeamlist = mListView;


            inflater = LayoutInflater.from(mContext);
            headView = (LinearLayout) inflater.inflate(R.layout.layout_headview, null);
            loadmore = (RelativeLayout) headView.findViewById(R.id.loadmore);
            check = (TextView) headView.findViewById(R.id.look);
            mIv = (ImageView) headView.findViewById(R.id.next);
            loadmore.setOnClickListener(new View.OnClickListener() {
                @Override


                public void onClick(View v) {


                        changeShow();




                }


            });
            mListView.addFooterView(headView, null, false);

        }





        public void setAdapterData(ArrayList<TeamInfo> allObjects) {
           mAllObjects = allObjects;
            mShowObjects.clear();
            if (mAllObjects != null) {
                if (mAllObjects.size() <= DEFAULT_SHOW_COUNT) {
                    headView.setVisibility(View.GONE);
                    mShowObjects.addAll(mAllObjects);

                } else {
                    headView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < DEFAULT_SHOW_COUNT; i++) {
                        mShowObjects.add(mAllObjects.get(i));

                    }

                }

            }
            notifyDataSetChanged();
            setListViewHeightBasedOnChildren(mTeamlist);

        }


        @Override


        public int getCount() {
            int showCount = 0;
            if (mShowObjects != null) {
                showCount = mShowObjects.size();

            }
            return showCount;

        }


        @Override

        public TeamInfo getItem(int position) {
            TeamInfo object = null;
            if (mShowObjects != null) {
                object = mShowObjects.get(position);

            }
            return object;

        }


        @Override


        public long getItemId(int position) {
            return position;

        }


        private void changeShow() {
            if (headView.getVisibility() == View.GONE) {
                headView.setVisibility(headView.VISIBLE);
            }
            mShowObjects.clear();
            if (shrink) {
                shrink = !shrink;
                isForecast=!isForecast;

                mShowObjects.addAll(mAllObjects);
              //  headView.setVisibility(View.GONE);
                check.setText("收起");
                mIv.setImageResource(R.mipmap.sq);


            } else {
                if(isForecast) {
                    headView.setVisibility(View.VISIBLE);
                    for (int i = 0; i < DEFAULT_SHOW_COUNT; i++) {
                        mShowObjects.add(mAllObjects.get(i));
                    }
                    isForecast=!isForecast;

                    }else{

                        for (int i = 0; i <mAllObjects.size() ; i++) {
                            mShowObjects.add(mAllObjects.get(i));

                        }
                    }
                shrink = !shrink;
                check.setText("查看更多团购");
                mIv.setImageResource(R.mipmap.next_iv);

            }



            notifyDataSetChanged();
            setListViewHeightBasedOnChildren(mTeamlist);

        }


        /**
         * 当ListView外层有ScrollView时，需要动态设置ListView高度
         *
         * @param listView
         */
        public void setListViewHeightBasedOnChildren(MyListView listView) {
            if (listView == null) return;
            ListAdapter listAdapter = listView.getAdapter();
            if (listAdapter == null) {
                return;
            }
            int totalHeight = 0;
            for (int i = 0; i < listAdapter.getCount(); i++) {
                View listItem = listAdapter.getView(i, null, listView);
                listItem.measure(0, 0);
                totalHeight += listItem.getMeasuredHeight();
            }
            ViewGroup.LayoutParams params = listView.getLayoutParams();
            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
            listView.setLayoutParams(params);
        }
    }
}
